﻿using System.Text;

namespace XLZF.MicroServices.Common;

public class FileHelper
{
    public static FileInfo[] GetFiles(string directoryPath)
    {
        if (!IsExistDirectory(directoryPath))
        {
            throw new DirectoryNotFoundException();
        }
        var root = new DirectoryInfo(directoryPath);
        return root.GetFiles();
    }
    public static bool IsExistDirectory(string directoryPath)
    {
        return Directory.Exists(directoryPath);
    }
    public static string ReadFile(string Path)
    {
        string s;
        if (!File.Exists(Path))
            s = "不存在相应的目录";
        else
        {
            var f2 = new StreamReader(Path, Encoding.Default);
            s = f2.ReadToEnd();
            f2.Close();
            f2.Dispose();
        }
        return s;
    }
    public static void FileMove(string OrignFile, string NewFile)
    {
        File.Move(OrignFile, NewFile);
    }
    public static void CreateDir(string dir)
    {
        if (dir.Length == 0) return;
        if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);
    }

    public static int GetRandom(int minNum, int maxNum)
    {
        var seed = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
        return new Random(seed).Next(minNum, maxNum);
    }

    #region 校验文件类型
    readonly static string[] badext = { "exe", "msi", "bat", "com", "sys", "aspx", "asax", "ashx" };
    public static string ProExt(string ext)
    {
        if (string.IsNullOrEmpty(ext)) return "";
        if (badext.Contains(ext)) throw new Exception("危险文件");
        if (ext.First() == '.') return ext;
        return "." + ext;
    }
    #endregion

    /// <summary>
    /// 合并文件
    /// </summary>
    /// <param name="infileName">文件路径合集</param>
    /// <param name="outfileName">完成文件路径</param>
    public static void CombineFile(string[] infileName, string outfileName)
    {
        int b;

        int n = infileName.Length;

        FileStream[] fileIn = new FileStream[n];

        using (FileStream fileOut = new FileStream(outfileName, FileMode.Create))
        {
            for (int i = 0; i < n; i++)
            {
                try
                {
                    fileIn[i] = new FileStream(infileName[i], FileMode.Open);

                    while ((b = fileIn[i].ReadByte()) != -1)

                        fileOut.WriteByte((byte)b);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    fileIn[i].Close();
                }
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="srcPath"></param>
    public static void DelectDir(string srcPath)
    {
        try
        {
            DirectoryInfo dir = new DirectoryInfo(srcPath);

            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录

            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)            //判断是否文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                    subdir.Delete(true);          //删除子目录和文件
                }
                else
                {
                    File.Delete(i.FullName);      //删除指定文件
                }
            }

            Directory.Delete(srcPath);
        }
        catch (Exception e)
        {
            throw;
        }
    }
}
